<?php
/*
 *
 *  form_zakazky_edit.php
 *
 *
 */

$select = "select "
          ."  z.datum_zadani as zadani, "
          ."  z.termin_odevzdani as odevzdani, "
          ."   '' as predani, "
          ."   z.cena_celkem as cena, "
          ."   z.naklady, "
          ."   z.cena_celkem / z.naklady as koef, "
          ."   max(pz.cislo_objednavky) as co, "
          ."   z.cislo_objednavky as cz, "
          ."   pr.alias as zadal, "
          ."   k.obchodni_jmeno as klient, "
          ."   z.projekt, "
          ."   z.popis_zakazky, "
          ."   sort.nazev as sort, "
          ."   tech.nazev as tech, "
          ."   sum(pz.metru_ctverecnich) as mc, "
          ."   pracovnik.alias as account "
          ." from "
          ."   klient as k "
          ."   join zakazka as z "
          ."    on (k.id = z.klient_id) "
          ."   join pracovnik as pr "
          ."    on (z.zakazku_zadal = pr.id) "
          ."   join podzakazka pz "
          ."    on (pz.zakazka_id = z.id) "
          ."   join sortiment as sort "
          ."    on (pz.sortiment = sort.id) "
          ."   join technologie as tech "
          ."    on (pz.technologie = tech.id) "
          ."   join pracovnik "
          ."    on (k.account = pracovnik.id) "
          ."   where (z.valid = 1) ";

if (isset($_POST["datum_zadani"]) && ($_POST["datum_zadani"] != ""))
{
  $select .= " and (z.datum_zadani = ".$_POST["datum_zadani"].")";
}

if (isset($_POST["termin_odevzdani"]) && ($_POST["termin_odevzdani"] != ""))
{
  $select .= " and (z.termin_odevzdani = ".$_POST["termin_odevzdani"].")";
}

if (isset($_POST["datum_predani"]) && ($_POST["datum_predani"] != ""))
{
  //$select .= " and (z.cena_celkem = ".$_POST["datum_predani"].")";
}

if (isset($_POST["cena"]) && ($_POST["cena"] != ""))
{
  $select .= " and (z.cena_celkem = ".$_POST["cena"].")";
}

if (isset($_POST["naklady"]) && ($_POST["naklady"] != ""))
{
  $select .= " and (z.naklady = ".$_POST["naklady"].")";
}

if (isset($_POST["koef"]) && ($_POST["koef"] != ""))
{
  $select .= " and (z.cena_celkem / z.naklady = ".$_POST["koef"].")";
}

if (isset($_POST["cz"]) && ($_POST["cz"] != ""))
{
  $select .= " and (z.cislo_objednavky like '%".$_POST["cz"]."%')";
}

if (isset($_POST["zadal"]) && ($_POST["zadal"] != ""))
{
  $select .= " and (pr.alias like '%".$_POST["zadal"]."%')";
}

if (isset($_POST["account"]) && ($_POST["account"] != ""))
{
  $select .= " and (pracovnik.alias like '%".$_POST["account"]."%')";
}

if (isset($_POST["klient"]) && ($_POST["klient"] != ""))
{
  $select .= " and (k.obchodni_jmeno like '%".$_POST["klient"]."%')";
}

if (isset($_POST["technologie"]) && ($_POST["technologie"] != ""))
{
  $select .= " and (tech.nazev like '%".$_POST["technologie"]."%')";
}

if (isset($_POST["metru_ctverecnich"]) && ($_POST["metru_ctverecnich"] != ""))
{
  $select .= " and (sum(pz.metru_ctverecnich) = ".$_POST["metru_ctverecnich"].")";
}

if (isset($_POST["projekt"]) && ($_POST["projekt"] != ""))
{
  $select .= " and (z.projekt like '%".$_POST["projekt"]."%')";
}

if (isset($_POST["popis"]) && ($_POST["popis"] != ""))
{
  $select .= " and (z.popis_zakazky like '%".$_POST["popis"]."%')";
}

if (isset($_POST["sortiment"]) && ($_POST["sortiment"] != ""))
{
  $select .= " and (sort.nazev like '%".$_POST["sortiment"]."%')";
}

if (isset($_POST["typ_zakazky"]) && ($_POST["typ_zakazky"] != ""))
{
  //$select .= " and (sort.nazev like '%".$_POST["typ_zakazky"]."%')";
}

//////////////////////////////
//    GROUP BY
//////////////////////////////
$select .=  " group by z.id ";

if (isset($_GET["sort"]))
{
  $select = $select." order by ".$_GET["sort"]." ".$_GET["d"];
}
else
{
  $select = $select." order by z.datum_zadani desc ";
}

//echo $select;
if (!$res = MySQL_Query($select, $spojeni))
{
  echo "chyba".MySQL_ErrNo()." ".mysql_error($spojeni);
}

?>
<style type="text/css">
<!--
.style7 {color: #000000}
-->
</style>

<form name="form" method="post" action="index.php?type=base&det=editz">
<table width="1500"  cellspacing="0" bordercolor="#FFFFFF" bgcolor="#FFFFFF">
<tr bgcolor="#FF9900">
  <td width="86" height="49"><div align="center" class="style7"><a href="index.php?type=base&det=editz&sort=datum_zadani<?php if ($_GET["d"] == "asc") echo "&d=desc"; else echo "&d=asc";  ?>">Datum zad&aacute;n&iacute; </a></div></td>
  <td width="119"><div align="center" class="style7"><a href="index.php?type=base&det=editz&sort=termin_odevzdani<?php if ($_GET["d"] == "asc") echo "&d=desc"; else echo "&d=asc";  ?>">Term&iacute;n odevzd&aacute;n&iacute; </a></div></td>
  <td width="118"><div align="center" class="style7">Datum p&#345;ed&aacute;n&iacute; </div></td>
  <td width="113"><div align="center" class="style7"><a href="index.php?type=base&det=editz&sort=cz<?php if ($_GET["d"] == "asc") echo "&d=desc"; else echo "&d=asc";  ?>">&#268;&iacute;slo zak&aacute;zky </a></div></td>
  <td width="115"><div align="center" class="style7"><a href="index.php?type=base&det=editz&sort=klient<?php if ($_GET["d"] == "asc") echo "&d=desc"; else echo "&d=asc";  ?>">Klient</a></div></td>
  <td width="77"><div align="center" class="style7"><a href="index.php?type=base&det=editz&sort=zadal<?php if ($_GET["d"] == "asc") echo "&d=desc"; else echo "&d=asc";  ?>">Zadal</a></div></td>
  <td width="77"><div align="center" class="style7"><a href="index.php?type=base&det=editz&sort=account<?php if ($_GET["d"] == "asc") echo "&d=desc"; else echo "&d=asc";  ?>">Account</a></div></td>
  <td width="89"><div align="center" class="style7"><a href="index.php?type=base&det=editz&sort=cena<?php if ($_GET["d"] == "asc") echo "&d=desc"; else echo "&d=asc";  ?>">Cena</a></div></td>
  <td width="116"><div align="center" class="style7"><a href="index.php?type=base&det=editz&sort=naklady<?php if ($_GET["d"] == "asc") echo "&d=desc"; else echo "&d=asc";  ?>">N&aacute;klady</a></div></td>
  <td width="80"><div align="center" class="style7"><a href="index.php?type=base&det=editz&sort=koef<?php if ($_GET["d"] == "asc") echo "&d=desc"; else echo "&d=asc";  ?>">Koeficient</a></div></td>
  <td width="73"><div align="center" class="style7"><a href="index.php?type=base&det=editz&sort=technologie<?php if ($_GET["d"] == "asc") echo "&d=desc"; else echo "&d=asc";  ?>">Technologie</a></div></td>
  <td width="60"><div align="center" class="style7"><a href="index.php?type=base&det=editz&sort=metru_ctverecnich<?php if ($_GET["d"] == "asc") echo "&d=desc"; else echo "&d=asc";  ?>">m2</a></div></td>
  <td width="77"><div align="center" class="style7">Projekt</div></td>
  <td width="77"><div align="center" class="style7">Popis</div></td>
  <!-- 1.1.10 -->
  <td width="77"><div align="center" class="style7"><a href="index.php?type=base&det=editz&sort=sortiment<?php if ($_GET["d"] == "asc") echo "&d=desc"; else echo "&d=asc";  ?>">Sortiment</a></div></td>
  <td width="112"><div align="center" class="style7">Typ zak&aacute;zky </div></td>
  <!-- 1.1.4 -->
</tr>

<tr bgcolor="#E2E2E2">

  <td><div align="center">
    <input type="text" size="10" name="datum_zadani"
        onchange="document.forms.form.action='index.php?type=base&det=editz';document.forms.form.submit()"/>
  </div></td>

  <td><div align="center">
    <input type="text" size="10" name="termin_odevzdani"
        onchange="document.forms.form.action='index.php?type=base&det=editz';document.forms.form.submit()"/>
  </div></td>

  <td><div align="center">
    <input type="text" size="10" name="datum_predani"
        onchange="document.forms.form.action='index.php?type=base&det=editz';document.forms.form.submit()"/>
  </div></td>

  <td><div align="center">
    <input type="text" size="10" name="cz"
        onchange="document.forms.form.action='index.php?type=base&det=editz';document.forms.form.submit()"/>
  </div></td>

  <td><div align="center">
    <input type="text" size="10" name="klient"
        onchange="document.forms.form.action='index.php?type=base&det=editz';document.forms.form.submit()"/>
  </div></td>

  <td><div align="center">
    <input type="text" size="10" name="zadal"
        onchange="document.forms.form.action='index.php?type=base&det=editz';document.forms.form.submit()"/>
  </div></td>

  <td><div align="center">
    <input type="text" size="10" name="account"
        onchange="document.forms.form.action='index.php?type=base&det=editz';document.forms.form.submit()"/>
  </div></td>

  <td><div align="center">
    <input type="text" size="10" name="cena"
        onchange="document.forms.form.action='index.php?type=base&det=editz';document.forms.form.submit()"/>
  </div></td>

  <td><div align="center">
    <input type="text" size="10" name="naklady"
        onchange="document.forms.form.action='index.php?type=base&det=editz';document.forms.form.submit()"/>
  </div></td>

  <td><div align="center">
    <input type="text" size="10" name="koef"
        onchange="document.forms.form.action='index.php?type=base&det=editz';document.forms.form.submit()"/>
  </div></td>

  <td><div align="center">
    <input type="text" size="10" name="technologie"
        onchange="document.forms.form.action='index.php?type=base&det=editz';document.forms.form.submit()"/>
  </div></td>

  <td><div align="center">
    <input type="text" size="10" name="metru_ctverecnich"
        onchange="document.forms.form.action='index.php?type=base&det=editz';document.forms.form.submit()"/>
  </div></td>

  <td><div align="center">
    <input type="text" size="10" name="projekt"
        onchange="document.forms.form.action='index.php?type=base&det=editz';document.forms.form.submit()"/>
  </div></td>

  <td><div align="center">
    <input type="text" size="10" name="popis"

        onchange="document.forms.form.action='index.php?type=base&det=editz';document.forms.form.submit()"/>
  </div></td>

  <td><div align="center">
    <input type="text" size="10" name="sortiment"
        onchange="document.forms.form.action='index.php?type=base&det=editz';document.forms.form.submit()"/>
  </div></td>

  <td><div align="center">
    <input type="text" size="10" name="typ_zakazky"
        onchange="document.forms.form.action='index.php?type=base&det=editz';document.forms.form.submit()"/>
  </div></td>

</tr>

<tr height="4" bgcolor="#888888">
  <td></td><td></td>
  <td></td><td></td>
  <td></td><td></td>
  <td></td><td></td>
  <td></td><td></td>
  <td></td><td></td>
  <td></td><td></td>
  <td></td><td></td>
</tr>

<?php
  $i = 1;
  while ($pole = MySQL_Fetch_Array($res))
  {
    $select = "select tz.typ as typ from  zakazka as z,  vztah_zakazka_typ as v, typ_zakazky as tz "
    ." where (cislo_objednavky = '".$pole["cz"]."') and (v.zakazka_id = z.id) and (v.typ_id = tz.id)";
    //echo $select;

    if (!$res_typ_zakazy = MySQL_Query($select, $GLOBALS["spojeni"]))
    {
      echo "chyba".MySQL_ErrNo()." ".mysql_error($spojeni);
    }

    echo "<tr>";
    echo "<td>".($pole["zadani"] == "" ? "" : StrFTime("%d.%m.", strtotime($pole["zadani"])))."</td>\n";
    echo "<td>".($pole["odevzdani"] == "" ? "" : StrFTime("%d.%m.", strtotime($pole["odevzdani"])))."</td>\n";
    echo "<td>".($pole["predani"] == "" ? "" : StrFTime("%d.%m.", strtotime($pole["predani"])))."</td>\n";
    $odkaz = $pole["cz"];
    $c = getAlphaOfPZ($pole["co"]);
    if ($c != "A")
      $odkaz .= " A-$c";

    //echo "<td><a onclick=\"window.open('vyber_pz.php?pz=".$pole["cz"]."','_blank','width=400,height=300'); return false;\" href='index.php?type=base&det=pridat_zakazku&editz=".$pole["cz"]."'>$odkaz</a></td>\n";
    echo "<td><a onclick=\"disp('vyber$i', ".(ord($c)-64).", 1);return false;\" href='index.php?type=base&det=pridat_zakazku&editz=".$pole["cz"]."'>$odkaz</a></td>\n";
    echo "<iframe src=\"vyber_pz.php?pz=".$pole["cz"]."\" class=\"vyber\" id=\"vyber$i\"\"></iframe>\n";

    echo "<td>".$pole["klient"]."</td>\n";
    echo "<td>".$pole["zadal"]."</td>\n";
    echo "<td>".$pole["account"]."</td>\n";
    echo "<td>".$pole["cena"]."</td>\n";
    echo "<td>".$pole["naklady"]."</td>\n";

    $koef = sprintf("%.3f", $pole["koef"]);
    echo "<td>$koef</td>\n";

    echo "<td>".$pole["tech"]."</td>\n";

    $mc = sprintf("%.2f", $pole["mc"]);
    echo "<td>$mc</td>\n";
    echo "<td>".$pole["projekt"]."</td>\n";
    echo "<td>".$pole["popis_zakazky"]."</td>\n";
    echo "<td>".$pole["sort"]."</td>\n";
    echo "<td>";

    while ($pole_typ_zakazky = MySQL_Fetch_Array($res_typ_zakazy))
    {
      echo " ".$pole_typ_zakazky["typ"];
    }
    echo "</td>\n";
    echo "</tr>\n";
    
    $i++;
  }

?>

</table>
</form>